$(function(){
    var loginData = JSON.parse(web.readCookie("loginData"));
    var  orderSetting = {
        isAuto:false,
        isAutoTj:10,
        isPeople:false,
        isPeopleTj:10,
        configId:getQueryString("configId"),
        init:function(){          

            if(this.configId){
                this.editInit();
            }else{
                this.chaneelData();
                this.isAutoTj = $("input[name='autoSplitMainCondition']:checked").val();
                this.isPeopleTj = $("input[name='manualAssignMainCondition']:checked").val();
            }                   

            this.eventBind();
        },
        eventBind:function(){
            var that = this;
            $('#autoRule').on('click',function(){
                that.autoRule();
            });


            $('.btn_fath').on('click',function(){
                var _this = $(this);
                var curType = _this.attr('data-type');
                if(_this.hasClass('on')){
                    _this.removeClass('on');
                    that[curType] = false;
                }else{
                    _this.addClass('on');
                    that[curType] = true;
                }

                that.pageChange();
                
            });

            $('.level_ele_1').on('click','input',function(){
                var curTt = $(this).attr('data-tt');

                if(curTt == 'auto'){
                    that.isAutoTj = $("input[name='autoSplitMainCondition']:checked").val();
                }

                if(curTt == 'people'){
                    that.isPeopleTj = $("input[name='manualAssignMainCondition']:checked").val();
                }

                that.pageChange();
                
            });

            $('.sub_reson').on('click','.del_radio',function(){                
                $(this).parent().parent().find('input').prop('checked',false);                
            });

             

            $('#submitConfig').on('click',function(){                     
                that.configSubmit();
            })

            $('#cancelConfig').on('click',function(){
                that.removeIframe(); 
            })
            
        },
        chaneelData:function(){
           web.doAjax(root.approvedServiceProviders.findChannelFromDockListByName,'post',{}, function (data) {
            if(data.code == "0") {                       
              
                    var channelHtml = '';
                    if(!data.data||data.data.length == 0){
                        channelHtml = '<option value="0">请选择渠道</option>';                           
                    } else {      
                        channelHtml += '<option value="0">请选择渠道</option>';                           
                        data.data.forEach((item) => {
                            channelHtml += '<option value="'+item.channelId+'">'+item.channelName+'</option>';
                        })
                    
                    }
                    $('#channelObj').html(channelHtml) 
                }
            },2);
        },
        autoRule:function(){ 
            layer.open({
                type: 1,
                title:"自动分配订单规则",
                skin:"radius1",
                offset:'100px',
                area: ['320', '340px'], //宽高
                content:$('.auto_rule_ct').html(),
                btn:['我知道了'],
            });
        },
        pageChange:function(){
            if(this.isAuto){
                $('.auto_level_1').show();
                $('.auto_step_1').hide();

                this.isAutoTj == 20 ? $('.auto_level_2').show() : $('.auto_level_2').hide();
               
            }else{
                $('.auto_level_1').hide();
                $('.auto_step_1').show();
            };

            if(this.isPeople){
                $('.peo_level_1').show();
                $('.peo_step_1').hide();

                this.isPeopleTj == 20 ? $('.peo_level_2').show() : $('.peo_level_2').hide();                       
            }else{
                $('.peo_level_1').hide();
                $('.peo_step_1').show();
            }
        },
        checkBoxVal:function(){
            var checkVal = '';
            $("input[name='manualAssignProviderRange']").each(function(){ 
                if($(this).prop("checked")){
                    checkVal += $(this).val()+","
                }
            })                   
            
            return checkVal;
        },
        editInit:function(){
            var that = this;
            web.doAjax(root.serviceProviderManagement.configGet,'get',{id:Number(this.configId)}, function (data) {
                if(data.code == "0") {  
                    var curData = data.data;  
                    var selHtml = '<option value="'+curData.channelId+'" checked>'+curData.channelName+'</option>'
                    $('#channelObj').html(selHtml); 

                    that.isAuto = curData.autoSplitStatus && curData.autoSplitStatus==10 ? true : false;
                    if(that.isAuto){
                        $(".btn_fath[data-type='isAuto']").addClass('on');
                        that.isAutoTj = curData.autoSplitMainCondition;
                        if(that.isAutoTj == 20){
                            $("input[name='autoSplitMainCondition']").eq(1).prop("checked",true);
                            var curInd =  curData.autoSplitBuyerCondition && curData.autoSplitBuyerCondition==20 ? 1 : 0;
                            $("input[name='autoSplitBuyerCondition']").eq(curInd).prop("checked",true);
                        }                                
                        
                    }else{
                        that.isAuto = false;
                    }

                    that.isPeople = curData.manualAssignStatus && curData.manualAssignStatus==10 ? true : false;
                    if(that.isPeople){
                        $(".btn_fath[data-type='isPeople']").addClass('on');
                        that.isPeopleTj = curData.manualAssignMainCondition;
                        var outInd = that.isPeopleTj == 20 ? 1 : 0;
                        $("input[name='manualAssignMainCondition']").eq(outInd).prop("checked",true);
                        if(that.isPeopleTj == 20){
                            if(curData.manualAssignBuyerCondition==20 || curData.manualAssignBuyerCondition==10){
                                var curInd = curData.manualAssignBuyerCondition==20 ? 1 : 0;
                                $("input[name='manualAssignBuyerCondition']").eq(curInd).prop("checked",true);
                            }                                    
                           
                            if(curData.manualAssignProductCondition==20 || curData.manualAssignProductCondition==10){
                                var curInd2 = curData.manualAssignProductCondition == 20 ? 1 : 0;                                      
                                $("input[name='manualAssignProductCondition']").eq(curInd2).prop("checked",true);
                            }
                            
                        } 
                        
                        var checkArr = curData.manualAssignProviderRange.split(',');                               
                        if(checkArr.length-1 >0){
                            $("input[name='manualAssignProviderRange']").each(function(){                                                                       
                            if(checkArr.indexOf($(this).attr('data-val'))!=-1) {
                                $(this).prop("checked",true);
                            }
                            })
                        }                              

                        
                    }else{
                        that.isPeople = false;
                    }
                    

                    
                    that.pageChange();
                }else{
                   layer.msg(data.msg); 
                }
            });  
        },
        submitValudate:function(){                   
            var parame = {};
            var channelId = $("#channelObj").val();
            
            if(channelId==0){
                layer.msg('请选择渠道', {icon: 1, time: 1000});
                return false;
            }
            parame.channelId = channelId;
            
            if(!this.isAuto && !this.isPeople){
                layer.msg('自动分单和人工分单必须开启一个', {icon: 1, time: 1000});
                return false;
            }

            if(this.isAuto){
                if(this.isAutoTj == 20){
                    var ordP = $("input[name='autoSplitBuyerCondition']:checked").val();
                    if(!ordP){
                        layer.msg('请至少设置一个自动分单条件', {icon: 1, time: 1000}); 
                        return false; 
                    }

                    parame.autoSplitBuyerCondition = ordP; 
                }
                parame.autoSplitMainCondition = this.isAutoTj; 
                parame.autoSplitStatus = 10;
            }else{
                parame.autoSplitStatus = 20;
            }


            if(this.isPeople){
                if(this.isPeopleTj == 20){
                    var ordP = $("input[name='manualAssignBuyerCondition']:checked").val();
                    var ordP2 = $("input[name='manualAssignProductCondition']:checked").val();
                    if(!ordP  &&  !ordP2){
                        layer.msg('请至少设置一个人工分单条件', {icon: 1, time: 1000}); 
                        return false; 
                    }                   
                    
                    parame.manualAssignBuyerCondition = ordP ? ordP :0 ; 
                    parame.manualAssignProductCondition = ordP2 ? ordP2 :0 ; 
                    
                }


                var ordP3 = this.checkBoxVal();  
                if(!ordP3.length){
                    layer.msg('请选择可分配服务商', {icon: 1, time: 1000}); 
                    return false; 
                }
                parame.manualAssignProviderRange = ordP3;

                
                parame.manualAssignMainCondition = this.isPeopleTj; 
                parame.manualAssignStatus = 10;
            }else{
                parame.manualAssignStatus = 20;
            }

            return parame;
           
        },
        configSubmit:function(){
            var that = this;
            var parame = this.submitValudate();
            var url = root.serviceProviderManagement.configSave;
            if(!parame){
                return;
            }

            parame.operationName = loginData.user.name;

            if(this.configId){
                parame.id = this.configId; 
                url = root.serviceProviderManagement.configUpdate;
            }
            web.doAjax(url,'post',parame, function (data) {
                if(data.code == "0") {  
                    layer.msg('提交成功'); 
                    parent.systemConfigurationList();
                    setTimeout( function () {
                        that.removeIframe();
                    },700);
                }else{
                   layer.msg(data.msg); 
                }
            },1);
        },
        removeIframe(){
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    }
    
    orderSetting.init();

});